package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Lugar;
import com.lostreseditores.sabersai.model.dto.LugarDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class LugarView {
    private InputText txtLugacoddanen;
    private InputText txtLugadescripv;
    private InputText txtLugaidn_Lugar;
    private InputText txtLugaidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<LugarDTO> data;
    private LugarDTO selectedLugar;

    public LugarView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            LugarDTO lugarDTO = (LugarDTO) e.getObject();

            if (txtLugacoddanen == null) {
                txtLugacoddanen = new InputText();
            }

            txtLugacoddanen.setValue(lugarDTO.getLugacoddanen());

            if (txtLugadescripv == null) {
                txtLugadescripv = new InputText();
            }

            txtLugadescripv.setValue(lugarDTO.getLugadescripv());

            if (txtLugaidn_Lugar == null) {
                txtLugaidn_Lugar = new InputText();
            }

            txtLugaidn_Lugar.setValue(lugarDTO.getLugaidn_Lugar());

            if (txtLugaidn == null) {
                txtLugaidn = new InputText();
            }

            txtLugaidn.setValue(lugarDTO.getLugaidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtLugacoddanen != null) {
            txtLugacoddanen.setValue(null);
            txtLugacoddanen.setDisabled(true);
        }

        if (txtLugadescripv != null) {
            txtLugadescripv.setValue(null);
            txtLugadescripv.setDisabled(true);
        }

        if (txtLugaidn_Lugar != null) {
            txtLugaidn_Lugar.setValue(null);
            txtLugaidn_Lugar.setDisabled(true);
        }

        if (txtLugaidn != null) {
            txtLugaidn.setValue(null);
            txtLugaidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Lugar entity = null;

        try {
            Integer lugaidn = new Integer(txtLugaidn.getValue().toString());
            entity = BusinessDelegatorView.getLugar(lugaidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtLugacoddanen.setDisabled(false);
            txtLugadescripv.setDisabled(false);
            txtLugaidn_Lugar.setDisabled(false);
            txtLugaidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtLugacoddanen.setValue(entity.getLugacoddanen());
            txtLugacoddanen.setDisabled(false);
            txtLugadescripv.setValue(entity.getLugadescripv());
            txtLugadescripv.setDisabled(false);
            txtLugaidn_Lugar.setValue(entity.getLugar().getLugaidn());
            txtLugaidn_Lugar.setDisabled(false);
            txtLugaidn.setValue(entity.getLugaidn());
            txtLugaidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveLugar(FacesUtils.checkLong(
                    txtLugacoddanen), FacesUtils.checkString(txtLugadescripv),
                FacesUtils.checkInteger(txtLugaidn),
                FacesUtils.checkInteger(txtLugaidn_Lugar));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteLugar(FacesUtils.checkInteger(
                    txtLugaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateLugar(FacesUtils.checkLong(
                    txtLugacoddanen), FacesUtils.checkString(txtLugadescripv),
                FacesUtils.checkInteger(txtLugaidn),
                FacesUtils.checkInteger(txtLugaidn_Lugar));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtLugaidn == null) {
                txtLugaidn = new InputText();
            }

            txtLugaidn.setValue(selectedLugar.getLugaidn());

            BusinessDelegatorView.deleteLugar(FacesUtils.checkInteger(
                    txtLugaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedLugar);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long lugacoddanen, String lugadescripv,
        Integer lugaidn, Integer lugaidn_Lugar) throws Exception {
        try {
            BusinessDelegatorView.updateLugar(lugacoddanen, lugadescripv,
                lugaidn, lugaidn_Lugar);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("LugarView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtLugacoddanen() {
        return txtLugacoddanen;
    }

    public void setTxtLugacoddanen(InputText txtLugacoddanen) {
        this.txtLugacoddanen = txtLugacoddanen;
    }

    public InputText getTxtLugadescripv() {
        return txtLugadescripv;
    }

    public void setTxtLugadescripv(InputText txtLugadescripv) {
        this.txtLugadescripv = txtLugadescripv;
    }

    public InputText getTxtLugaidn_Lugar() {
        return txtLugaidn_Lugar;
    }

    public void setTxtLugaidn_Lugar(InputText txtLugaidn_Lugar) {
        this.txtLugaidn_Lugar = txtLugaidn_Lugar;
    }

    public InputText getTxtLugaidn() {
        return txtLugaidn;
    }

    public void setTxtLugaidn(InputText txtLugaidn) {
        this.txtLugaidn = txtLugaidn;
    }

    public List<LugarDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataLugar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<LugarDTO> lugarDTO) {
        this.data = lugarDTO;
    }

    public LugarDTO getSelectedLugar() {
        return selectedLugar;
    }

    public void setSelectedLugar(LugarDTO lugar) {
        this.selectedLugar = lugar;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
